﻿--[[
   @brief    
   @author   rickonzhang
   @version  1.0
   @date     2015-08-25
--]]

retry_get_list_mgr = 
{
    friend_retry_obj_ids = {};
    group_retry_obj_ids = {};
    msg_retry_obj_ids = {};

    is_get_friend_list_success = true;
    is_get_group_list_success = true;
};

function retry_get_list_mgr.init()
    base_utility.log("[retry_get_list_mgr] init ....");
end

function retry_get_list_mgr.update_ui()
    local show_retry_friend = "false";
    if retry_get_list_mgr.is_get_friend_list_success == false then
        show_retry_friend = "true"
    end
    for index, id in ipairs(retry_get_list_mgr.friend_retry_obj_ids) do
        ui_utility.setproperty_by_id(id, "Visible", show_retry_friend);
    end

    local show_retry_group = "false";
    if retry_get_list_mgr.is_get_group_list_success == false then
        show_retry_group = "true"
    end
    -- test
    -- show_retry_group = "true";
    for index, id in ipairs(retry_get_list_mgr.group_retry_obj_ids) do
        ui_utility.setproperty_by_id(id, "Visible", show_retry_group);
        if "true" == show_retry_group then
            local parent_id = ui_utility.get_ctrl_parent_id(id);
            local loading_id = ui_utility.get_ctrl_id_by_path(parent_id, "Ctn_Loading");
            ui_utility.setproperty_by_id(loading_id, "Visible", "false");
        end
    end

    local show_retry_msg = "false";
    if show_retry_friend == "true" and show_retry_group == "true" then
        show_retry_msg = "true";
    end
    for index, id in ipairs(retry_get_list_mgr.msg_retry_obj_ids) do
        ui_utility.setproperty_by_id(id, "Visible", show_retry_msg);
    end
end

function retry_get_list_mgr.on_friend_retry_obj_create(uilib, ctrl_id)
    table.insert(retry_get_list_mgr.friend_retry_obj_ids, ctrl_id);
    retry_get_list_mgr.update_ui();
end

function retry_get_list_mgr.on_group_retry_obj_create(uilib, ctrl_id)
    table.insert(retry_get_list_mgr.group_retry_obj_ids, ctrl_id);
    retry_get_list_mgr.update_ui();
end

function retry_get_list_mgr.on_msg_list_retry_obj_create(uilib, ctrl_id)
    table.insert(retry_get_list_mgr.msg_retry_obj_ids, ctrl_id);
    retry_get_list_mgr.update_ui();
end

function retry_get_list_mgr.on_get_fiend_list_error(event_id, json_str)
    retry_get_list_mgr.is_get_friend_list_success = false;
    retry_get_list_mgr.update_ui();
end

function retry_get_list_mgr.on_get_group_list_error(event_id, json_str)
    retry_get_list_mgr.is_get_group_list_success = false;
    retry_get_list_mgr.update_ui();    
end

function retry_get_list_mgr.click_on_retry_btn(uilib, ctrl_id)
    local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local pp_id = ui_utility.get_ctrl_parent_id(p_id);
    local obj_id = ui_utility.get_ctrl_parent_id(pp_id);

    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");

    -- 重新拉取好友列表
    if retry_get_list_mgr.is_in_friend_retry_ids(obj_id) then
        retry_get_list_mgr.is_get_friend_list_success = true;
        social_network:get_friend_list();

    -- 重新拉取群组列表
    elseif retry_get_list_mgr.is_in_group_retry_ids(obj_id) then
        retry_get_list_mgr.is_get_group_list_success = true;
        social_network:get_group_list();

    -- 重新拉取好友、群组列表
    elseif retry_get_list_mgr.is_in_msg_retry_ids(obj_id) then
        retry_get_list_mgr.is_get_friend_list_success = true;
        social_network:get_friend_list();
        retry_get_list_mgr.is_get_group_list_success = true;
        social_network:get_group_list();
    end

    retry_get_list_mgr.update_ui();
end

function retry_get_list_mgr.is_in_friend_retry_ids(id)
    for index, obj_id in ipairs(retry_get_list_mgr.friend_retry_obj_ids) do
        if obj_id == id then
            return true;
        end
    end
    return false;
end

function retry_get_list_mgr.is_in_group_retry_ids(id)
    for index, obj_id in ipairs(retry_get_list_mgr.group_retry_obj_ids) do
        if obj_id == id then
            return true;
        end
    end
    return false;
end

function retry_get_list_mgr.is_in_msg_retry_ids(id)
    for index, obj_id in ipairs(retry_get_list_mgr.msg_retry_obj_ids) do
        if obj_id == id then
            return true;
        end
    end
    return false;
end

retry_get_list_mgr.init();
